package win.zxshuo.www.spider;


import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import win.zxshuo.www.model.BookModel;
import win.zxshuo.www.model.ChapterModelMongoDB;
import win.zxshuo.www.model.chapters.ChapterModel;
import win.zxshuo.www.service.BookService;
import win.zxshuo.www.service.ChapterMongoService;
import win.zxshuo.www.service.ChapterService;
import win.zxshuo.www.service.TargetUrlService;
import win.zxshuo.www.utils.StringUtils;


import java.util.*;
import java.util.stream.Collectors;


@Component
public class JieqiSpider extends BaseSpider {
    Log logger = LogFactory.getLog(getClass());

    @Autowired
    BookService bookService;

    @Autowired
    ChapterService chapterService;

    @Autowired
    ChapterMongoService chapterMongoService;

    @Autowired
    TargetUrlService targetUrlService;




    public void getBookIndex(String url,BookModel bookModel) throws Exception{
        try {
            Document document = getPageContent(url);
            //TODO 这里将会把数据从MySQL转换到MongoDB
            List<ChapterModelMongoDB> chapterModels = getIndexMap(document,url);
            if (chapterModels.size()>0){
                chapterMongoService.addNewChapterForBook(bookModel,chapterModels);
            }else {
                logger.info(url+"这个url并不是一个可用的杰奇采集，请知晓");
            }
        }catch (Exception e){
            throw new Exception(e.getCause());
        }
    }

    public void getBookIndex(String url) throws Exception{
        try {
            Document document = getPageContent(url);
            Map<String,String> cralerdBookinfo = getBookinfo(document);
            BookModel bookModel = bookService.getBookModelBy(cralerdBookinfo.get("title"),cralerdBookinfo.get("author"));
            if (bookModel != null){
                targetUrlService.addNew(bookModel,url);
                List<ChapterModelMongoDB> chapterModels = getIndexMap(document,url);
//                chapterService.addNewChapterForBook(bookModel,chapterModels);
            }
        }catch (Exception e){
            throw new Exception(e.getCause());
        }

    }

    public void startCrawler(){
        List<ChapterModel> chapterModels = chapterService.getUncrawlerdUrl();
        if (chapterModels.size()>0){
            for (ChapterModel chapterModel:chapterModels){
                try{
                    Document document = getPageContent(chapterModel.getSpiderUrl());
                    Element content = document.selectFirst("#content");
                    if (content != null){
                        String contentStr = StringUtils.changeBrToP(content.html());
                        chapterModel.setContent(contentStr);
                        chapterModel.setChapterWordNum(contentStr.length());
                        chapterModel.setSpiderStatus("1");
                        chapterService.updateChapterBy(chapterModel);
                        bookService.addBookWordNum(contentStr.length(),chapterModel.getBookId());
                    }
                }catch (Exception e){

                    logger.error(e.getCause());
                }

            }

        }
    }


    public void startCrawlerChapter(ChapterModel chapterModel,BookModel bookModel){
        try{
            Document document = getPageContent(chapterModel.getSpiderUrl());
            Element content = document.selectFirst("#content");
            if (content != null){
                String contentStr = StringUtils.changeBrToP(content.html());
                chapterModel.setContent(contentStr);
                chapterModel.setChapterWordNum(contentStr.length());
                chapterModel.setSpiderStatus("1");
                chapterService.updateChapterBy(chapterModel,bookModel);
                bookService.addBookWordNum(contentStr.length(),chapterModel.getBookId());
            }else {
                logger.info("采集的章节内容为空，疑似IP已经被屏蔽了");
            }
        }catch (Exception e){
            logger.info("书籍id"+chapterModel.getBookId()+ "  章节："+chapterModel.getId()+"采集失败");
            logger.error(e.getCause());
        }

    }





    /**
     * 通过html来获取目录
     * @param document
     */
    public List<ChapterModelMongoDB> getIndexMap(Document document){
        List<ChapterModelMongoDB> indexList = new ArrayList();
        List<ChapterModelMongoDB> sortedList = new ArrayList();
        Element indexBox = document.selectFirst("#list");
        if (indexBox != null){
            Elements indexLists = indexBox.select("dd");
            for (Element box:indexLists){
                Element aElement = box.selectFirst("a");
                if (aElement != null){
                    String title = aElement.text();
                    String link = aElement.attr("href");
                    if (title != null && link != null){
                        ChapterModelMongoDB chapterModel = new ChapterModelMongoDB();
                        chapterModel.setTitle(title);
                        chapterModel.setSpiderUrl(link);
                        chapterModel.setChapterOrder(StringUtils.getTitleNum(title));
                        indexList.add(chapterModel);
                    }
                }
            }
            sortedList = indexList.stream().
                    sorted((c1,c2)->c1.getChapterOrder().compareTo(c2.getChapterOrder())).collect(Collectors.toList());
        }else {
            logger.info("并没有找到id=list的东西，你是不是弄错了规则？");
        }

        System.out.println("开始打印排序后的章节了。");
        sortedList = makeChapterUnique(sortedList);
        for (ChapterModelMongoDB chapterModel:sortedList){
            System.out.println(chapterModel.getTitle());
            System.out.println(chapterModel.getChapterOrder());
        }
        return sortedList;
    }

    /**
     * 通过html来获取目录 获取完整的url
     * @param document
     * @param requestUrl
     * @return
     */
    public List<ChapterModelMongoDB> getIndexMap(Document document,String requestUrl){
        List<ChapterModelMongoDB> indexList = new ArrayList();
        List<ChapterModelMongoDB> sortedList = new ArrayList();
        Element indexBox = document.selectFirst("#list");
        if (indexBox != null){
            Elements indexLists = indexBox.select("dd");
            for (Element box:indexLists){
                Element aElement = box.selectFirst("a");
                if (aElement != null){
                    String title = aElement.text();
                    String link = aElement.attr("href");
                    if (title != null && link != null){
                        ChapterModelMongoDB chapterModel = new ChapterModelMongoDB();
                        chapterModel.setTitle(title);
                        chapterModel.setSpiderUrl(StringUtils.makeUrlFull(link,requestUrl));
                        chapterModel.setChapterOrder(StringUtils.getTitleNum(title));
                        indexList.add(chapterModel);
                    }
                }
            }
            sortedList = indexList.stream().
                    sorted((c1,c2)->c1.getChapterOrder().compareTo(c2.getChapterOrder())).collect(Collectors.toList());
        }else {
            logger.info("并没有找到id=list的东西，你是不是弄错了规则？");
        }

        System.out.println("开始打印排序后的章节了。");
        sortedList = makeChapterUnique(sortedList);
        for (ChapterModelMongoDB chapterModel:sortedList){
            System.out.println(chapterModel.getTitle());
            System.out.println(chapterModel.getChapterOrder());
        }
        return sortedList;
    }

    /**
     * 排除同样章节的
     * @param chapterModelList
     * @return
     */
    public List<ChapterModelMongoDB> makeChapterUnique(List<ChapterModelMongoDB> chapterModelList){
        Integer i = 0;
        while (i+1<chapterModelList.size()){
            if (chapterModelList.get(i).getSpiderUrl().equals(chapterModelList.get(i+1).getSpiderUrl())){
                chapterModelList.remove(chapterModelList.get(i));
            }
            i ++;
        }
        return chapterModelList;
    }

    /**
     * 通过html来获取书籍信息
     * @param document
     */
    public Map<String,String> getBookinfo(Document document){
        Map<String,String> bookinfo = new HashMap<>();
        Element mainInfo = document.selectFirst("#maininfo");
        Element info = mainInfo.selectFirst("#info");
        String author = "";
        String bookTitle = info.selectFirst("h1").text();
        bookinfo.put("title",bookTitle);
        Elements ps = info.select("p");
        for (Element element:ps){
            String str = element.text();
            if (str.contains("作") && str.contains("者")){
                String[] strs = element.text().split("：");
                if (strs.length <2){
                    strs = element.text().split(":");
                }
                if (strs.length>=2){
                    author = strs[1];
                    bookinfo.put("author",author);
                }
            }

        }
        System.out.println(author);
        System.out.println(bookTitle);

        return bookinfo;

    }

}
